package com.example.zhinongbao.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.zhinongbao.MainApplication;
import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.GoodsAdapter;
import com.example.zhinongbao.bean.Good;
import com.example.zhinongbao.greendao.GoodDao;
import com.example.zhinongbao.utils.FragmentUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页
 */
public class HomeFragment extends Fragment {
    // 商品分类的按钮，分别是推荐，水果蔬菜，米面粮油，农资农具
    private final List<Button> buttonList = new ArrayList<>();
    // 搜索商品的列表
    private final List<Good> searchGoods = new ArrayList<>();
    // 商品的RecyclerView滑动视图
    private RecyclerView recyclerView;
    // 商品适配器
    private GoodsAdapter goodsAdapter;
    // 搜索框编辑栏
    private EditText edit_search;
    // 所有商品的列表
    private List<Good> AllGoods = new ArrayList<>();
    // 商品数据库
    private GoodDao goodDao;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取商品数据库
        goodDao = MainApplication.getInstance().getGoodDaoSession().getGoodDao();
        initView();
    }

    private void initView() {
        edit_search = getView().findViewById(R.id.edit_search);
        recyclerView = getView().findViewById(R.id.home_recycler_view);
        // 商品展示的RecyclerView使用2列瀑布流显示
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        goodsAdapter = new GoodsAdapter(searchGoods);
        recyclerView.setAdapter(goodsAdapter);
        // 把各个分类按钮添加到按钮列表中
        buttonList.add(getView().findViewById(R.id.btn_fruits));
        buttonList.add(getView().findViewById(R.id.btn_rice));
        buttonList.add(getView().findViewById(R.id.btn_tools));
        selectGoodsType(getView().findViewById(R.id.btn_fruits));
        for (Button button : buttonList) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击不同分类按钮时，选择其类别的商品
                    selectGoodsType(button);
                }
            });
        }

        getView().findViewById(R.id.btn_sell).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入卖货页面
                FragmentUtils.getInstance().switchFragment(R.id.nav_sell);
            }
        });

        getView().findViewById(R.id.btn_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 搜索商品
                searchGoods();
            }
        });
    }

    /**
     * 选择商品类型，推荐、水果蔬菜、米面粮油、农资农具
     *
     * @param button
     */
    @SuppressLint("NotifyDataSetChanged")
    private void selectGoodsType(Button button) {
        // 点击某个分类的时候，该分类高亮，其他分类背景变回白色
        button.setSelected(true);
        for (Button button1 : buttonList) {
            if (button1 != button) {
                button1.setSelected(false);
            }
        }
        // 初始化全商品列表和搜索商品列表
        AllGoods.clear();
        searchGoods.clear();
        // 查询所有商品
        AllGoods = goodDao.loadAll();
        // 根据按钮id获取商品类型
        int goodType = 1;
        if (button.getId() == R.id.btn_fruits) {
            goodType = 1;
        } else if (button.getId() == R.id.btn_rice) {
            goodType = 2;
        } else if (button.getId() == R.id.btn_tools) {
            goodType = 3;
        }
        // 根据商品类型筛选出来展示
        for (Good good : AllGoods) {
            if (good.getType() == goodType) {
                searchGoods.add(good);
            }
        }
        // 通知数据刷新，视图刷新
        goodsAdapter.notifyDataSetChanged();
    }

    /**
     * 搜索商品
     */
    private void searchGoods() {
        if (AllGoods.isEmpty() ||
                edit_search.getText() == null ||
                edit_search.getText().toString().isEmpty()) {
            return;
        }
        // 先清空搜索的商品列表
        searchGoods.clear();
        String searchName = edit_search.getText().toString();
        // 把筛选出来的商品添加到搜索的商品列表中
        for (Good good : AllGoods) {
            if (good.getLabel().contains(searchName)) {
                searchGoods.add(good);
            }
        }
        // 通知数据刷新，视图刷新
        goodsAdapter.notifyDataSetChanged();
    }

}
